package com.fwj.core.common.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: fwj
 * @Data: 2022/2/20 23:19
 * @Version 1.0
 * @Description:
 */


public class CookieUtil {

    /**
     * @Description 签发Cookie
     * @Param:
     * @Author fwj
     * @return void
     * @Createtime 2022/2/20
     */
    public static void addCookie(
            HttpServletResponse response,
            String domain, String path, String name,
            String value, int maxAge, boolean httpOnly){
        //创建Cookie
        Cookie cookie = new Cookie(name, value);
        //设置cookie
        cookie.setDomain(domain);
        //设置Cookie路径
        cookie.setPath(path);
        //设置Cookie最大生命周期
        cookie.setMaxAge(maxAge);
        //设置Cookie是否值
        cookie.setHttpOnly(httpOnly);
        //返回客户端需求Cookie
        response.addCookie(cookie);
    }

    /**
     * @Description 获取Cookie参数 , 待优化
     * @Param:
     * @Author fwj
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @Createtime 2022/2/20
     */
    public static Map<String,String> readCookie(HttpServletRequest request, String ... cookieNames){
        Map<String,String> cookieMap = new HashMap<String,String>();
        Cookie[] cookies = request.getCookies();
        //录入Cookie到Map
        if(CommonUtils.isNotEmpty(cookies)){
            for(Cookie cookie : cookies){
                String cookieName = cookie.getName();
                String cookieValue = cookie.getValue();
                for(int i = 0; i < cookieNames.length; i++){
                    if(cookieNames[i].equals(cookieName)){
                        cookieMap.put(cookieName,cookieValue);
                        break;
                    }
                }
            }
        }
        return cookieMap;
    }

    /**
     * @Description 获取Cookie参数 , 待优化
     * @Param:
     * @Author fwj
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @Createtime 2022/2/20
     */
    public static String getCookie(HttpServletRequest request, String cookieName){
        Cookie[] cookies = request.getCookies();
        if(CommonUtils.isNotEmpty(cookies)){
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(cookieName)){
                    return cookie.getValue();
                }
            }
        }
        return "";
    }


    /**
     * @Description 获取Cookie参数 , 待优化
     * @Param:
     * @Author fwj
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @Createtime 2022/2/20
     */
    public static Map<String,String> getAllCookie(HttpServletRequest request){
        Map<String,String> cookieMap = new HashMap<String,String>();
        Cookie[] cookies = request.getCookies();
        //录入Cookie到Map
        if(CommonUtils.isNotEmpty(cookies)){
            for(Cookie cookie : cookies){
                String cookieName = cookie.getName();
                String cookieValue = cookie.getValue();
                cookieMap.put(cookieName,cookieValue);
            }
        }
        return cookieMap;
    }
}
